<script setup lang="ts">
import { requiredCheck, singleIpCheck, validatePortCheck } from "@/utils/verify";
import { ref } from "vue";
import { addData, editData } from "@/api/modules/resource/sample-finger";
import SelectAppInput from "@/components/SelectAppInput/index.vue";
import SelectFingerTextData from "@/components/SelectFingerTextData/index.vue";

interface Props {
  title?: string;
  rowData?: Object;
}

const props = withDefaults(defineProps<Props>(), {
  title: "新增",
  rowData: undefined
});
defineOptions({
  name: "UpdateForm" // 默认策略
});
import useUpdate from "@/hooks/useUpdate";
import { ifaceAll, ifacemodeUpdate } from "@/api/modules/dynamic-camouflage/line-config";
import SelectLinksInput from "@/components/SelectLinksInput/index.vue";

const emits = defineEmits(["success"]);
const { updateFormData, updateFormRef, updateFormRules, submit, showHandle, openHandle, closeHandle } = useUpdate({
  rules: {
    name: [...requiredCheck("请输入指纹名称")],
    app: [...requiredCheck("请选择协议")],
    port: [...requiredCheck("请输入端口"), { validator: validatePortCheck }],
    text: [...requiredCheck("请输入关键信息")]
  },
  defaultData: {
    fingerarr: []
  }
});
const ifaces = ref([]);

async function getIface() {
  try {
    const res = await ifaceAll();
    console.log(res);
    if (res) {
      ifaces.value = Object.values(res);
      console.log(ifaces.value);
    }
    Object.values(res);
  } catch (error) {}
}

async function submitCb() {
  await submit({
    addApi: addData,
    updateApi: editData,
    sendData: {
      name: updateFormData.name,
      text: updateFormData.text,
      port: updateFormData.port,
      app: updateFormData.app,
      appId: updateFormData.app,
      id: updateFormData?.id
    }
  });
  emits("success");
}
</script>

<template>
  <div
    class="update-form mg-r-sm"
    style="display: inline-block"
    @click="
      openHandle({
        row: {
          ...rowData
        },
        id: props.rowData?.id
      });

      getIface();
    "
  >
    <slot>
      <el-button type="primary">新增</el-button>
    </slot>
  </div>
  <base-dialog @confirm="submitCb()" width="600px" :title="title" v-model="showHandle">
    <div class="details">
      <div class="details-main">
        <el-form ref="updateFormRef" :model="updateFormData" :rules="updateFormRules" label-width="120px" label-suffix="：">
          <el-form-item label="主机名" prop="name">
            <el-input style="width: 220px" v-model="updateFormData.name" auto-complete="off" placeholder="请输入主机名" />
          </el-form-item>
          <el-form-item label="指纹" prop="name">
            <div class="v-box" style="align-items: self-start">
              <div class="h-box">
                <SelectFingerTextData>
                  <el-button type="primary">选择指纹样例</el-button>
                </SelectFingerTextData>
                <!--                <el-button type="primary">新增</el-button>-->
              </div>
              <div class="mg-t-sm">
                <div class="h-box mg-b-sm" v-for="(item, index) in updateFormData.fingerarr" :key="item?.id">
                  <el-input v-model="item.port" placeholder="端口" style="width: 80px"></el-input>
                  <el-input placeholder="指纹信息" v-model="item.text" class="mg-l-sm" style="width: 120px"></el-input>
                  <el-icon class="mg-l-sm cursor-pointer">
                    <Remove />
                  </el-icon>
                </div>
              </div>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <template #footer-more-button></template>
  </base-dialog>
</template>

<style lang="scss" scoped></style>
